package com.example.erp.service.system.impl;

import com.example.erp.config.CommonConfig;
import com.example.erp.dto.systemDTO.MenuDTO;
import com.example.erp.mapper.system.MenuMapper;
import com.example.erp.pojo.system.Menu;
import com.example.erp.service.system.MenuService;
import com.example.erp.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MenuServiceA implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Result list(MenuDTO menuDTO) {
        List<Menu> list = menuMapper.list(menuDTO);
        return Result.success(list);
    }

    @Override
    public Result add(Menu menu) {
        if(!menu.getMenuType().equals(CommonConfig.menuType.F.name()) && menuMapper.getByName(menu.getMenuName()) != null){
            return Result.error(CommonConfig.resultMsg.MENUNAMEQUERY.getDescription());
        }
        menu.setMenuId(getMenuId(menu) + 1);
        Integer count = menuMapper.add(menu);
        if (count != null && count >= 1) {
            return Result.successMsg(menu.getId(),CommonConfig.msgTYPE.ADD.getDescription());
        } else {
            return Result.error(CommonConfig.msgTYPE.ADDERR.getDescription());
        }
    }

    @Transactional
    @Override
    public Result edit(Menu menu) {
        if(!menu.getMenuType().equals(CommonConfig.menuType.F.name()) && menuMapper.getByIdName(menu.getId(),menu.getMenuName()) != null){
            return Result.error(CommonConfig.resultMsg.MENUNAMEQUERY.getDescription());
        }
        if(menu.getMenuId() == menu.getParentId()){
            return Result.error(CommonConfig.msgTYPE.EDITERROR.getDescription());
        }
        Integer count = menuMapper.edit(menu);
        if (count != null && count == 1) {
            return Result.successMsg(CommonConfig.msgTYPE.EDIT.getDescription());
        } else {
            return Result.error(CommonConfig.msgTYPE.EDITERROR.getDescription());
        }
    }

    @Override
    public Result remove(Long id) {
        Integer count = menuMapper.remove(id);
        if (count != null && count == 1) {
            return Result.successMsg(CommonConfig.msgTYPE.REMOVE.getDescription());
        } else {
            return Result.error(CommonConfig.msgTYPE.REMOVEERROR.getDescription());
        }
    }

    public Long getMenuId(Menu menu){
        List<Menu> list = menuMapper.listAll();
        Long maxMenuId = list.stream()
                .filter(item -> {
                    if(!menu.getMenuType().equals(item.getMenuType())){
                        return false;
                    }
                    if(menu.getMenuType().equals(CommonConfig.menuType.M.name())){
                        return (item.getParentId() == null || item.getParentId() == 0) && menu.getId() != item.getId();
                    }else if(menu.getMenuType().equals(CommonConfig.menuType.C.name())){
                        return item.getParentId() != null && menu.getId() != item.getId();
                    }else if(menu.getMenuType().equals(CommonConfig.menuType.F.name())){
                        return item.getParentId() >= 100 && menu.getId() != item.getId();
                    }else{
                        return false;
                    }
                })
                .map(Menu::getMenuId)
                .max(Long::compareTo)
                .orElseGet(() -> {
                    if (menu.getMenuType().equals(CommonConfig.menuType.M.name())) {
                        return 0L;
                    } else if (menu.getMenuType().equals(CommonConfig.menuType.C.name())) {
                        return 99L;
                    } else if (menu.getMenuType().equals(CommonConfig.menuType.F.name())) {
                        return 999L;
                    } else {
                        return 0L;
                    }
                });
        return maxMenuId;
    }
}
